﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using AddressBook.DAO;
using AddressBook.Entity;
using System.Text;

public partial class Login : System.Web.UI.Page
{
    static VerifyCodeDAO dao = new VerifyCodeDAO();
    static Base_EmployeeDAO daoEmployee = new Base_EmployeeDAO();

    protected void Page_Load(object sender, EventArgs e)
    {
        ResultEntity result = new ResultEntity();
        result.Name = "login";

        if (!string.IsNullOrEmpty(Request.QueryString["phone"]) && !string.IsNullOrEmpty(Request.QueryString["verify"]))
        {
            VerifyCode code = dao.GetInfo(" where RevPhone = '" + Request.QueryString["phone"] + "' and CodeNum = '" + Request.QueryString["verify"] + "' and RevUserName='0' order by createtime desc");
            if (code != null)
            {
                if (code.CreateTime.AddMinutes(10) > DateTime.Now)
                {
                    Globle.ExecuteNonQuery("update VerifyCode set RevUserName='1' where  RevPhone = '" + Request.QueryString["phone"] + "'");
                    result.ResultCode = "0";//登录成功后发回用户信息

                    Base_Employee employ = daoEmployee.GetInfo(" where phone = '" + Request.QueryString["phone"] + "'");
                    if (employ != null)
                    {
                        if (!employ.isDelete)
                        {
                            result.ResultCode = "0";
                            StringBuilder sb = new StringBuilder();
                            sb.Append("{");
                            sb.AppendFormat("\"EmployID\":\"{0}\",", employ.EmployID);
                            sb.AppendFormat("\"FstName\":\"{0}\",", employ.FstName);
                            sb.AppendFormat("\"SecName\":\"{0}\",", employ.SecName);
                            sb.AppendFormat("\"Phone\":\"{0}\",", employ.Phone);
                            sb.AppendFormat("\"DeptID\":\"{0}\",", employ.DeptID);
                            sb.AppendFormat("\"Address\":\"{0}\",", employ.Address);
                            sb.AppendFormat("\"Email\":\"{0}\",", employ.Email);
                            sb.AppendFormat("\"FamiryTel\":\"{0}\",", employ.FamiryTel);
                            sb.AppendFormat("\"Fax\":\"{0}\",", employ.Fax);
                            sb.AppendFormat("\"HeadUrl\":\"{0}\",", employ.HeadUrl);
                            sb.AppendFormat("\"Sex\":\"{0}\",", employ.Sex);
                            sb.AppendFormat("\"Station\":\"{0}\",", employ.Station);
                            sb.AppendFormat("\"Tel\":\"{0}\",", employ.Tel);
                            sb.AppendFormat("\"Property\":\"{0}\"", employ.Property);
                            sb.Append("}");

                            result.Contents = sb.ToString();
                        }
                        else
                        {
                            result.Desc = "该用户已被删除！";
                            result.ResultCode = "-4";
                        }
                    }
                    else
                    {
                        result.Desc = "该用户不存在！";
                        result.ResultCode = "-4";
                    }

                }
                else
                {
                    result.ResultCode = "-1";
                    result.Desc = "验证码已过期！";
                }
            }
            else
            {
                result.ResultCode = "-3";
                result.Desc = "验证码不正确或者你还没有请求验证码！";
            }
        }
        else
        {
            result.ResultCode = "-2";
            result.Desc = "参数不正确！";

        }
        Response.Write(Common.ReturnResult(result));
    }
}
